package cn.toolck;

import cn.toolck.config.ConsumerConfig;
import cn.toolck.service.ConsumerService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ConsumerMain {
    public static void main(String[] args) throws Exception {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ConsumerConfig.class);
        applicationContext.start();
        ConsumerService bean = applicationContext.getBean(ConsumerService.class);
        ExecutorService executorService = Executors.newFixedThreadPool(12);
        while (true) {
            executorService.execute(() -> {
                try {
                    startService(bean, executorService, () -> {
                        bean.callA("A1");
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            executorService.execute(() -> {
                try {
                    startService(bean, executorService, () -> {
                        bean.callA2("A2");
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            executorService.execute(() -> {
                try {
                    startService(bean, executorService, () -> {
                        bean.callA3("A13");
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            TimeUnit.MILLISECONDS.sleep(200);
        }
    }

    private static void startService(ConsumerService bean, ExecutorService executorService, Runnable runnable) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            executorService.execute(runnable);
        }
    }
}
